package org.fujene.structure;

public final class SearchRangeTuple{
	public static final byte RANGE_GREATER = 0;
	public static final byte RANGE_LESSOREQUAL = 1;
	public static final byte RANGE_EQUAL = 2;
	public static final byte RANGE_NOTEQUAL = 3;
	public static final byte RANGE_RANGE = 4;
	public static final byte RANGE_NOTRANGE = 5;
	public static final byte RANGE_DONTCARE = (byte)0xFF;

	private int rangeFrom, rangeTo;
	private boolean equalToFrom;

	private SearchRangeTuple(){}

	public static SearchRangeTuple greaterThan(int value){
		SearchRangeTuple newTuple = new SearchRangeTuple();
		newTuple.rangeFrom = value;
		newTuple.rangeTo = Integer.MAX_VALUE;
		newTuple.equalToFrom = true;
		return newTuple;
	}

	public static SearchRangeTuple lessThan(int value){
		SearchRangeTuple newTuple = new SearchRangeTuple();
		newTuple.rangeFrom = Integer.MIN_VALUE;
		newTuple.rangeTo = value;
		newTuple.equalToFrom = true;
		return newTuple;
	}

	public static SearchRangeTuple equalValue(int value){
		SearchRangeTuple newTuple = new SearchRangeTuple();
		newTuple.rangeFrom = value;
		newTuple.rangeTo = value;
		newTuple.equalToFrom = true;
		return newTuple;
	}

	public static SearchRangeTuple notEqualValue(int value){
		SearchRangeTuple newTuple = new SearchRangeTuple();
		newTuple.rangeFrom = value;
		newTuple.rangeTo = value;
		newTuple.equalToFrom = false;
		return newTuple;
	}

	public static SearchRangeTuple range(int valueFrom, int valueTo){
		SearchRangeTuple newTuple = new SearchRangeTuple();
		newTuple.rangeFrom = valueFrom;
		newTuple.rangeTo = valueTo;
		newTuple.equalToFrom = true;
		return newTuple;
	}

	public static SearchRangeTuple notRange(int valueFrom, int valueTo){
		SearchRangeTuple newTuple = new SearchRangeTuple();
		newTuple.rangeFrom = valueFrom;
		newTuple.rangeTo = valueTo;
		newTuple.equalToFrom = false;
		return newTuple;
	}

	public static SearchRangeTuple dontCare(){
		SearchRangeTuple newTuple = new SearchRangeTuple();
		newTuple.rangeFrom = Integer.MIN_VALUE;
		newTuple.rangeTo = Integer.MAX_VALUE;
		newTuple.equalToFrom = true;
		return newTuple;
	}
	
	public static SearchRangeTuple fromMessage(int rangeFrom, int rangeTo, boolean equalToFrom) {
		SearchRangeTuple newTuple = new SearchRangeTuple();
		newTuple.rangeFrom = rangeFrom;
		newTuple.rangeTo = rangeTo;
		newTuple.equalToFrom = equalToFrom;
		return newTuple;
	}

	public boolean isRange(int value){
		if(equalToFrom){
			if(rangeFrom == rangeTo && rangeFrom == value) return true;
			if(rangeFrom <= value && value <= rangeTo) return true;
		}else{
			if(rangeFrom == rangeTo && rangeFrom != value) return true;
			if(value < rangeFrom || rangeTo < value) return true;
		}
		return false;
	}
	
	public int rangeFrom() {
		return rangeFrom;
	}
	
	public int rangeTo() {
		return rangeTo;
	}
	
	public boolean equalToFrom() {
		return equalToFrom;
	}
}
